package com.qyj.guiyangdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qyj.guiyangdemo.entity.*;
import com.qyj.guiyangdemo.mapper.DeptMapper;
import com.qyj.guiyangdemo.mapper.OrdertMapper;
import com.qyj.guiyangdemo.service.OrderService;
import com.qyj.guiyangdemo.util.ResultUtil;
import com.qyj.guiyangdemo.util.TimeUtility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;

@Service
public class OrderServiceimpl extends ServiceImpl<OrdertMapper, WorkOrder> implements OrderService {
    @Autowired
    OrdertMapper ordertMapper;
    @Autowired
    DeptMapper deptMapper;
    @Override
    public Result getByOrderNo(String orderNo) {
        String byOrderNo = ordertMapper.getByOrderNo(orderNo);
        System.out.println("byOrderNo = " + byOrderNo);
        if (byOrderNo != null){
            return ResultUtil.error("orderNo已经存在");
        }
        return null;
    }

    @Override
    public ArrayList<WorkOrder> search(PageNum pageNum) {
        Page<WorkOrder> page = new Page<>(pageNum.getPage(), pageNum.getNum());
        Page<WorkOrder> workOrderPage = ordertMapper.selectPage(page,null);
        return new ArrayList<>(workOrderPage.getRecords());
    }

    @Override
    public boolean setdeptAndTime(Long orderId, Long deptId) {
        Timestamp time = TimeUtility.getTime();
        DeptInfo dept = deptMapper.selectOne(new QueryWrapper<DeptInfo>().eq("dept_id", deptId));
        if (dept==null){
            return false;
        }
        WorkOrder id = ordertMapper.selectOne(new QueryWrapper<WorkOrder>().eq("id", orderId));
        if (id==null){
            return false;
        }
        id.setFenpaiTime(time);
        id.setHandleDeptId(deptId);
        int i = ordertMapper.updateById(id);

        System.out.println("i = " + i);
        if (i==0){
            return false;
        }

        return true;
    }

    @Override
    public ArrayList<NumInfo> getByParamOne(Integer month) {
        return ordertMapper.getNumA(month);
    }

    @Override
    public ArrayList<NumInfo> getByParamTwo(Integer month) {
       return ordertMapper.getNumB(month);
    }
    @Override
    public ArrayList<NumInfo> getByParamThree(Integer month) {
        return ordertMapper.getNumC(month);
    }
}
